package cn.miju.ser.unionpay.controller;

import cn.miju.ser.framework.exception.ApiException;
import cn.miju.ser.unionpay.service.UnionPayService;
import cn.miju.ser.unionpay.vo.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <pre>
 * 平台银联云闪付开放接口
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-14 11:20
 */
@Api(tags = "银联支付")
@RestController
@RequestMapping("/unionpay")
public class UnionPayController {

    private final UnionPayService service;

    public UnionPayController(UnionPayService service) {
        this.service = service;
    }

    /**
     * 扫码支付下单
     * @param inVO {@link UpCreateQrcodeOrderInVO}
     * @return {@link UpCreateQrcodeOrderOutVO}
     */
    @ApiOperation("扫码支付下单")
    @ApiOperationSupport(order = 1, author = "zhuming")
    @PostMapping("/createQrcodeOrder")
    public UpCreateQrcodeOrderOutVO createQrcodeOrder(@RequestBody @Validated UpCreateQrcodeOrderInVO inVO) {
        return service.createQrcodeOrder(inVO);
    }

    /**
     * 查询订单
     * @param inVO {@link UpQueryOrderInVO}
     * @return {@link UpQueryOrderOutVO}
     */
    @ApiOperation("查询订单")
    @ApiOperationSupport(order = 2, author = "zhuming")
    @PostMapping("/queryOrder")
    public UpQueryOrderOutVO queryOrder(@RequestBody @Validated UpQueryOrderInVO inVO) {
        return service.queryOrder(inVO);
    }
    /**
     * 订单退款
     * @param inVO {@link UpRefundOrderInVO}
     * @return {@link UpRefundOrderOutVO}
     */
    @ApiOperation("订单退款")
    @ApiOperationSupport(order = 3, author = "zhuming")
    @PostMapping("/refundOrder")
    public UpRefundOrderOutVO refundOrder(@RequestBody @Validated UpRefundOrderInVO inVO) {
        UpRefundOrderOutVO outVO = service.refundOrder(inVO);
        if (StringUtils.isBlank(outVO.getTradeNo())) {
            throw new ApiException("退款受理失败！");
        }
        return outVO;
    }
}
